package cn.source.zk.service.impl;

import cn.source.zk.domain.dto.*;
import cn.source.zk.domain.entity.*;
import cn.source.zk.domain.vo.DbAggDataVO;
import cn.source.zk.mapper.DbBaseDataMapper;
import cn.source.zk.service.IDbBaseDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DbBaseDataServiceImpl implements IDbBaseDataService {

    @Autowired
    private DbBaseDataMapper baseDataMapper;

    /**
     * 根据时间类型查询平均值
     *
     * @param baseDataQueryDTO
     * @return
     */
    public List<Map<String, Object>> selectByDateAverage(BaseDataQueryDTO baseDataQueryDTO) {

        List<Map<String, Object>> list = new ArrayList<>();

        if (baseDataMapper.selectByDateAverage(baseDataQueryDTO) != null) {
            list.addAll(baseDataMapper.selectByDateAverage(baseDataQueryDTO));
        }
        return list;
    }

    /**
     * 根据时间类型查询最大值
     *
     * @param baseDataQueryDTO
     * @return
     */
    public List<Map<String, Object>> selectByDateMax(BaseDataQueryDTO baseDataQueryDTO) {
        List<Map<String, Object>> list = new ArrayList<>();

        if (baseDataMapper.selectByDateMax(baseDataQueryDTO) != null) {
            list.addAll(baseDataMapper.selectByDateMax(baseDataQueryDTO));
        }
        return list;
    }

    /**
     * 根据时间类型查询最小值
     *
     * @param baseDataQueryDTO
     * @return
     */
    public List<Map<String, Object>> selectByDateMin(BaseDataQueryDTO baseDataQueryDTO) {
        List<Map<String, Object>> list = new ArrayList<>();

        if (baseDataMapper.selectByDateMin(baseDataQueryDTO) != null) {
            list.addAll(baseDataMapper.selectByDateMin(baseDataQueryDTO));
        }
        return list;
    }

    /**
     * 查询出两个小时内没有更新的通道；
     *
     * @param
     */
    public List<BaseData> checkBaseData() {
        List<BaseData> list = baseDataMapper.checkBaseData();
        return list;
    }

    /**
     * 获取BaseData数据
     *
     * @param baseData2TableDTO
     * @return
     */
    public List<DbBaseData> getBaseDataList(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getBaseDataList(baseData2TableDTO);
    }

    @Override
    public List<DbAggDataVO> getAggData(DbAllIndicatorsDTO dbAllIndicatorsDTO) {
        // 查询基础表数据的瞬时数据
        List<Map<String, Object>> list = baseDataMapper.getMoments(dbAllIndicatorsDTO);

        DbAggDataVO dbAggDataVO = new DbAggDataVO();


        return Collections.emptyList();
    }

    @Override
    public List<DBIhra> getDbIhra(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbIhra(baseData2TableDTO);
    }

    @Override
    public List<DBIhrb> getDbIhrb(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbIhrb(baseData2TableDTO);
    }

    @Override
    public List<DBIhrc> getDbIhrc(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbIhrc(baseData2TableDTO);
    }

    @Override
    public List<DBIhrja> getDbIhrja(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbIhrja(baseData2TableDTO);
    }

    @Override
    public List<DBIhrjb> getDbIhrjb(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbIhrjb(baseData2TableDTO);
    }

    @Override
    public List<DBIhrjc> getDbIhrjc(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbIhrjc(baseData2TableDTO);
    }

    @Override
    public List<DBUhra> getDbUhra(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbUhra(baseData2TableDTO);
    }

    @Override
    public List<DBUhrb> getDbUhrb(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbUhrb(baseData2TableDTO);
    }

    @Override
    public List<DBUhrc> getDbUhrc(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbUhrc(baseData2TableDTO);
    }

    @Override
    public List<DBUhrja> getDbUhrja(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbUhrja(baseData2TableDTO);
    }

    @Override
    public List<DBUhrjb> getDbUhrjb(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbUhrjb(baseData2TableDTO);
    }

    @Override
    public List<DBUhrjc> getDbUhrjc(BaseData2TableDTO baseData2TableDTO) {
        return baseDataMapper.getDbUhrjc(baseData2TableDTO);
    }

    public static String process(String input, char resultChar) {
        // 将字符串转换为字符数组
        char[] chars = input.toCharArray();
        //"db_ihra"、"db_ihrja"

        if (input.length() <= 9) {
            chars[chars.length - 1] = resultChar;
        } else {
            // 替换倒数第三个字符为'b'
            chars[chars.length - 4] = resultChar;
        }


        // 返回处理后的字符串
        return new String(chars);
    }


    @Override
    public List<DBIhrDTO> getDbIhr(BaseData2TableDTO baseData2TableDTO) {

        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'a'));
        List<DBIhra> dbIhra = baseDataMapper.getDbIhra(baseData2TableDTO);
        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'b'));
        List<DBIhrb> dbIhrb = baseDataMapper.getDbIhrb(baseData2TableDTO);
        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'c'));
        List<DBIhrc> dbIhrc = baseDataMapper.getDbIhrc(baseData2TableDTO);

        // 收集所有不同的时间点
        Set<LocalDateTime> allTimedates = new HashSet<>();
        dbIhra.forEach(a -> allTimedates.add(a.getTimedate()));
        dbIhrb.forEach(b -> allTimedates.add(b.getTimedate()));
        dbIhrc.forEach(c -> allTimedates.add(c.getTimedate()));
        //
        // 按时间排序
        List<LocalDateTime> sortedTimedates = new ArrayList<>(allTimedates);
        Collections.sort(sortedTimedates, Collections.reverseOrder());

        // 创建时间到实体的映射
        Map<LocalDateTime, DBIhra> timeToTableA = dbIhra.stream()
                .collect(Collectors.toMap(DBIhra::getTimedate, a -> a, (a1, a2) -> a1));
        Map<LocalDateTime, DBIhrb> timeToTableB = dbIhrb.stream()
                .collect(Collectors.toMap(DBIhrb::getTimedate, b -> b, (b1, b2) -> b1));
        Map<LocalDateTime, DBIhrc> timeToTableC = dbIhrc.stream()
                .collect(Collectors.toMap(DBIhrc::getTimedate, c -> c, (c1, c2) -> c1));

        // 合并数据
        List<DBIhrDTO> result = new ArrayList<>();
        for (LocalDateTime time : sortedTimedates) {
            DBIhrDTO dto = new DBIhrDTO();
            dto.buildToIhra(timeToTableA.get(time), baseData2TableDTO.getFields());
            dto.buildToIhrb(timeToTableB.get(time), baseData2TableDTO.getFields());
            dto.buildToIhrc(timeToTableC.get(time), baseData2TableDTO.getFields());
            result.add(dto);
        }

        return result;
    }

    @Override
    public List<DBIhrjDTO> getDbIhrj(BaseData2TableDTO baseData2TableDTO) {

        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'a'));
        List<DBIhrja> dbIhrja = baseDataMapper.getDbIhrja(baseData2TableDTO);
        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'b'));
        List<DBIhrjb> dbIhrjb = baseDataMapper.getDbIhrjb(baseData2TableDTO);
        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'c'));
        List<DBIhrjc> dbIhrjc = baseDataMapper.getDbIhrjc(baseData2TableDTO);

        // 收集所有不同的时间点
        Set<LocalDateTime> allTimedates = new HashSet<>();
        dbIhrja.forEach(a -> allTimedates.add(a.getTimedate()));
        dbIhrjb.forEach(b -> allTimedates.add(b.getTimedate()));
        dbIhrjc.forEach(c -> allTimedates.add(c.getTimedate()));
        //
        // 按时间排序
        List<LocalDateTime> sortedTimedates = new ArrayList<>(allTimedates);
        Collections.sort(sortedTimedates);

        // 创建时间到实体的映射
        Map<LocalDateTime, DBIhrja> timeToTableA = dbIhrja.stream()
                .collect(Collectors.toMap(DBIhrja::getTimedate, a -> a, (a1, a2) -> a1));
        Map<LocalDateTime, DBIhrjb> timeToTableB = dbIhrjb.stream()
                .collect(Collectors.toMap(DBIhrjb::getTimedate, b -> b, (b1, b2) -> b1));
        Map<LocalDateTime, DBIhrjc> timeToTableC = dbIhrjc.stream()
                .collect(Collectors.toMap(DBIhrjc::getTimedate, c -> c, (c1, c2) -> c1));

        // 合并数据
        List<DBIhrjDTO> result = new ArrayList<>();
        for (LocalDateTime time : sortedTimedates) {
            DBIhrjDTO dto = new DBIhrjDTO();
            dto.buildToIhrja(timeToTableA.get(time), baseData2TableDTO.getFields());
            dto.buildToIhrjb(timeToTableB.get(time), baseData2TableDTO.getFields());
            dto.buildToIhrjc(timeToTableC.get(time), baseData2TableDTO.getFields());
            result.add(dto);
        }

        return result;
    }

    @Override
    public List<DBUhrDTO> getDbUhr(BaseData2TableDTO baseData2TableDTO) {
        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'a'));
        List<DBUhra> dbIhra = baseDataMapper.getDbUhra(baseData2TableDTO);
        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'b'));
        List<DBUhrb> dbIhrb = baseDataMapper.getDbUhrb(baseData2TableDTO);
        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'c'));
        List<DBUhrc> dbIhrc = baseDataMapper.getDbUhrc(baseData2TableDTO);

        // 收集所有不同的时间点
        Set<LocalDateTime> allTimedates = new HashSet<>();
        dbIhra.forEach(a -> allTimedates.add(a.getTimedate()));
        dbIhrb.forEach(b -> allTimedates.add(b.getTimedate()));
        dbIhrc.forEach(c -> allTimedates.add(c.getTimedate()));
        //
        // 按时间排序
        List<LocalDateTime> sortedTimedates = new ArrayList<>(allTimedates);
        Collections.sort(sortedTimedates);

        // 创建时间到实体的映射
        Map<LocalDateTime, DBUhra> timeToTableA = dbIhra.stream()
                .collect(Collectors.toMap(DBUhra::getTimedate, a -> a, (a1, a2) -> a1));
        Map<LocalDateTime, DBUhrb> timeToTableB = dbIhrb.stream()
                .collect(Collectors.toMap(DBUhrb::getTimedate, b -> b, (b1, b2) -> b1));
        Map<LocalDateTime, DBUhrc> timeToTableC = dbIhrc.stream()
                .collect(Collectors.toMap(DBUhrc::getTimedate, c -> c, (c1, c2) -> c1));

        // 合并数据
        List<DBUhrDTO> result = new ArrayList<>();
        for (LocalDateTime time : sortedTimedates) {
            DBUhrDTO dto = new DBUhrDTO();
            dto.buildToUhra(timeToTableA.get(time), baseData2TableDTO.getFields());
            dto.buildToUhrb(timeToTableB.get(time), baseData2TableDTO.getFields());
            dto.buildToUhrc(timeToTableC.get(time), baseData2TableDTO.getFields());
            result.add(dto);
        }

        return result;
    }

    @Override
    public List<DBUhrjDTO> getDbUhrj(BaseData2TableDTO baseData2TableDTO) {
        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'a'));
        List<DBUhrja> dbIhra = baseDataMapper.getDbUhrja(baseData2TableDTO);
        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'b'));
        List<DBUhrjb> dbIhrb = baseDataMapper.getDbUhrjb(baseData2TableDTO);
        baseData2TableDTO.setTableName(process(baseData2TableDTO.getTableName(), 'c'));
        List<DBUhrjc> dbIhrc = baseDataMapper.getDbUhrjc(baseData2TableDTO);

        // 收集所有不同的时间点
        Set<LocalDateTime> allTimedates = new HashSet<>();
        dbIhra.forEach(a -> allTimedates.add(a.getTimedate()));
        dbIhrb.forEach(b -> allTimedates.add(b.getTimedate()));
        dbIhrc.forEach(c -> allTimedates.add(c.getTimedate()));
        //
        // 按时间排序
        List<LocalDateTime> sortedTimedates = new ArrayList<>(allTimedates);
        Collections.sort(sortedTimedates);

        // 创建时间到实体的映射
        Map<LocalDateTime, DBUhrja> timeToTableA = dbIhra.stream()
                .collect(Collectors.toMap(DBUhrja::getTimedate, a -> a, (a1, a2) -> a1));
        Map<LocalDateTime, DBUhrjb> timeToTableB = dbIhrb.stream()
                .collect(Collectors.toMap(DBUhrjb::getTimedate, b -> b, (b1, b2) -> b1));
        Map<LocalDateTime, DBUhrjc> timeToTableC = dbIhrc.stream()
                .collect(Collectors.toMap(DBUhrjc::getTimedate, c -> c, (c1, c2) -> c1));

        // 合并数据
        List<DBUhrjDTO> result = new ArrayList<>();
        for (LocalDateTime time : sortedTimedates) {
            DBUhrjDTO dto = new DBUhrjDTO();
            dto.buildToUhrja(timeToTableA.get(time), baseData2TableDTO.getFields());
            dto.buildToUhrjb(timeToTableB.get(time), baseData2TableDTO.getFields());
            dto.buildToUhrjc(timeToTableC.get(time), baseData2TableDTO.getFields());
            result.add(dto);
        }

        return result;
    }


}
